﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LabMS.BLL;
using LabMS.Model;
using ExtendBLL;
using System.Collections.Generic;

namespace LabMS.Web.LabPersonnel
{
    public partial class EditResponsibility : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("RYGWZZGL");
            strPageUserType.Add("mana");
            if (Request.QueryString["ID"] == null)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            int ID = 0;
            if (!int.TryParse(Request.QueryString["ID"], out ID))
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            if (!IsPostBack)
            {
                LabDropdownlist();
                BindData(ID);
            }
        }

        //绑定所属实验室
        protected void LabDropdownlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> LabTemp = new List<LabMS.Model.Lab>();

            ParLab = lab.GetModelList("Lab_ParentID is null");
            SonLab = lab.GetModelList("Lab_ParentID is not null");
            LabTemp = lab.GetModelList("Lab_ParentID is null");

            int count = ParLab.Count, count1 = SonLab.Count;
            for (int i = 0; i < count; i++)
            {
                int ID = ParLab[i].ID;
                int m = 0;
                for (int j = 0; j < count1; j++)
                {
                    LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                    int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

                    if (ParentID == ID)
                    {
                        labModel.ID = SonLab[j].ID;
                        labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
                        LabTemp.Insert(i + m + 1, labModel);
                        m++;
                    }
                }
            }
            RespLabDDL.DataSource = LabTemp;
            RespLabDDL.DataBind();
            ListItem item = new ListItem("无", "0");
            RespLabDDL.Items.Insert(0, item);
        }

        private void BindData(int ID)
        {
            ExtendBLL.Responsibility responsibility = new ExtendBLL.Responsibility();
            ExtendModule.ResponsibilityAndOther responsibilityInfo = new ExtendModule.ResponsibilityAndOther();

            responsibilityInfo = responsibility.GetModel(ID);
            RespLabDDL.SelectedValue = responsibilityInfo.Lab_ID.ToString();
            RespName.Text = responsibilityInfo.Resp_Name;
            RespContent.Text = responsibilityInfo.Resp_Content;
        }

        protected void Submit_OnClick(object sender, EventArgs e)
        {
            #region 验证
            if (int.Parse(RespLabDDL.SelectedValue.Trim()) == 0)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "实验室名称不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(RespName.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "岗位职责名称不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(RespContent.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "职责内容不能为空!");
                return;
            }
            #endregion
            LabMS.Model.Responsibility responsibility = new LabMS.Model.Responsibility();
            LabMS.BLL.Responsibility responsibilityInfo = new LabMS.BLL.Responsibility();

            int RID = 0;
            if (!int.TryParse(Request.QueryString["ID"], out RID))
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            responsibility.ID = RID;
            responsibility.Resp_Lab = int.Parse(RespLabDDL.SelectedValue.Trim());
            responsibility.Resp_Name = RespName.Text.ToString().Trim();
            responsibility.Resp_Content = RespContent.Text.ToString().Trim();

            responsibilityInfo.Update(responsibility);
            LabMS.Common.JShelper.JSAlertAndRedirect(Page, "EditSuccess", "修改成功!", "ListResponsibility.aspx");
        }
    }
}
